package cz.uhk.ppro.controllers.admin;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import cz.uhk.ppro.entities.Uzivatel;
import cz.uhk.ppro.form.Login;
import cz.uhk.ppro.form.UzivatelForm;
import cz.uhk.ppro.services.LoginChecker;
import cz.uhk.ppro.services.UzivatelDbService;
import cz.uhk.ppro.validator.UzivatelValidator;

@Controller
@SessionAttributes({"skupiny", "uzivatele", "uzivatel"})
public class AdminUzivatelController {
	Login login;
	UzivatelDbService uzivatelService;
	LoginChecker loginChecker;
	UzivatelForm uzivatelForm;
	UzivatelValidator uzivatelValidator;
		
	@ModelAttribute("uzivatelForm")
	public UzivatelForm getUzivatelForm() {
		return new UzivatelForm();
	}
			
	@ModelAttribute("login")
	public Login getLogin() {
		return new Login();
	}
	
	@Autowired
	public void setUzivatelService (UzivatelDbService uzivatelService){
		this.uzivatelService = uzivatelService;
	}
	
	@Autowired
	public void setLoginChecker (LoginChecker loginChecker){
		this.loginChecker = loginChecker;
	}
	
	@Autowired
	public void setUzivatelValidator(UzivatelValidator uzivVal) {
		this.uzivatelValidator = uzivVal;
	}
	
	@RequestMapping("/adminUzivatel")
	public String users(Locale locale, ModelMap model, HttpSession session,
			@ModelAttribute("uzivatelForm") UzivatelForm uzivatelForm) throws NoSuchAlgorithmException {   
		if (loginChecker.checkLogin(session, "Admin")){
			
			uzivatelForm.setUzivatel(new Uzivatel()); 
			
			List<Uzivatel> uziv = uzivatelService.vypis();
			List<String> skupiny = new ArrayList<String>();
			skupiny.add("Skladnik");
			skupiny.add("Manager");
			skupiny.add("Admin");
			
			model.addAttribute("skupiny", skupiny);
			model.addAttribute("uzivatele",uziv);
			return "adminUzivatel";
		}else {
			return "accessDenied";
		}
	}
	
	@RequestMapping("/adminPridejUzivatel")
	public String add(@ModelAttribute("uzivatelForm") UzivatelForm uzivatelForm, BindingResult result,
			Locale locale, ModelMap model, HttpSession session) throws NoSuchAlgorithmException {
		uzivatelValidator.validate(uzivatelForm, result);
		if (result.hasErrors()) {
			return "adminUzivatel";
		 } else {
		if (loginChecker.checkLogin(session, "Admin")){
			
			uzivatelService.pridej(uzivatelForm.getJmeno(), uzivatelForm.getPrijmeni(), 
					uzivatelForm.getLogin(), uzivatelForm.getHeslo(), uzivatelForm.getSkupina());

			return "redirect:/adminUzivatel";
		}else {
			return "accessDenied";
		}}
	}
	
	
	@RequestMapping("/adminSmazUzivatel")
	public String minus(Locale locale, ModelMap model, HttpSession session,
			@ModelAttribute("uzivatelForm") UzivatelForm uzivatelForm,
			int id) throws NoSuchAlgorithmException {
		if (loginChecker.checkLogin(session, "Admin")){
			
			uzivatelService.smaz(uzivatelService.vrat(id));

			return "redirect:/adminUzivatel";
		}else {
			return "accessDenied";
		}
	}
	
	@RequestMapping(value = "/adminUpravUzivatel", method = RequestMethod.GET)
	public String edit(Locale locale, ModelMap model, HttpSession session,
			@ModelAttribute("uzivatelForm") UzivatelForm uzivatelForm,
			int id) throws NoSuchAlgorithmException {
		if (loginChecker.checkLogin(session, "Admin")){
					
			Uzivatel uzivatel = uzivatelService.vrat(id);
			uzivatelForm.setUzivatel(uzivatel);
			uzivatelForm.setOldPass(uzivatel.getHeslo());
			
			List<String> skupiny = new ArrayList<String>();
			skupiny.add("Skladnik");
			skupiny.add("Manager");
			skupiny.add("Admin");
			
			model.addAttribute("skupiny", skupiny);
			model.addAttribute("uzivatel", uzivatel);
			
			return "adminUpravUzivatel";
		}else {
			return "accessDenied";
		}
	}
	
	@RequestMapping(value = "/adminUpravUzivatel", method = RequestMethod.POST)
	public String editPost(@ModelAttribute("uzivatelForm") UzivatelForm uzivatelForm, BindingResult result,
			Locale locale, ModelMap model, HttpSession session) 
					throws NoSuchAlgorithmException {
		uzivatelValidator.validate(uzivatelForm, result);
		if (result.hasErrors()) {
			return "adminUpravUzivatel";
		 } else {
		if (loginChecker.checkLogin(session, "Admin")){
			
			Uzivatel uzivatel = uzivatelService.vrat(uzivatelForm.getId());
			if (uzivatelForm.getHeslo().compareTo(uzivatelForm.getOldPass())==0){
				uzivatelService.upravBezHesla(uzivatel, uzivatelForm.getJmeno(), uzivatelForm.getPrijmeni(),
						uzivatelForm.getLogin(), uzivatelForm.getSkupina());
			} else {
			uzivatelService.uprav(uzivatel, uzivatelForm.getJmeno(), uzivatelForm.getPrijmeni(),
					uzivatelForm.getLogin(), uzivatelForm.getHeslo(), uzivatelForm.getSkupina());
			}
			return "redirect:/adminUzivatel";
		}else {
			return "accessDenied";
		}}
	}
}
